//
//  UIButton+Extension.swift
//  Weibo10
//
//  Created by male on 15/10/14.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit

extension UIButton {
    
    /// 便利构造函数
    ///
    /// - parameter imageName:     图像名称
    /// - parameter backImageName: 背景图像名称
    ///
    /// - returns: UIButton
    convenience init(imageName: String, backImageName: String) {
        self.init()
        
        setImage(UIImage(named: imageName), forState: .Normal)
        setImage(UIImage(named: imageName + "_highlighted"), forState: .Highlighted)
        setBackgroundImage(UIImage(named: backImageName), forState: .Normal)
        setBackgroundImage(UIImage(named: backImageName + "_highlighted"), forState: .Highlighted)
        
        // 会根据背景图片的大小调整尺寸
        sizeToFit()
    }
    
    /// 便利构造函数
    ///
    /// - parameter title:     title
    /// - parameter color:     color
    /// - parameter imageName: imageName
    ///
    /// - returns: UIButton
    convenience init(title: String, color: UIColor, imageName: String) {
        self.init()
        
        setTitle(title, forState: .Normal)
        setTitleColor(color, forState: .Normal)
        
        setBackgroundImage(UIImage(named: imageName), forState: .Normal)
    }
    
    // 便利构造函数
    convenience init(title: String, fontSize: CGFloat, color:UIColor, imageName: String) {
        // 必须先实现父类的方法
        self.init()
        
        // 设置标题
        setTitle(title, forState: UIControlState.Normal)
        // 设置标题的颜色
        setTitleColor(color, forState: UIControlState.Normal)
        // 设置图片
        setImage(UIImage(named: imageName), forState: .Normal)
        // 设置字体
        titleLabel?.font = UIFont.systemFontOfSize(fontSize)
        // 自动适应大小
        sizeToFit()
        
    }
    
}
